const mongoose = require('../common/db');

const ArticleSchema = new mongoose.Schema({
    title: { type: String, required: true, minlength: 1, maxlength: 255 },
    content: { type: String, required: true, minlength: 1, maxlength: 2048 },
    author: { type: String, required: true }, // 文章作者直接从token中获取
    tags: { type: [String], required: true }, // 文章标签
    status: { type: String, enum: ['published', 'draft'], required: true }, // 文章状态，发布、草稿
}, { timestamps: true });

ArticleSchema.statics.query = async function (condition) {
    return await this.find(condition).exec();
};

ArticleSchema.statics.findByID = async function (id) {
    return await this.findOne({ _id: id }).exec();
};

ArticleSchema.statics.findByTitle = async function (title) {
    return await this.find({ title }).exec();
};


ArticleSchema.statics.findAll = async function () {
    return await this.find().exec();
};


const ArticleModel = mongoose.model('Article', ArticleSchema);

module.exports = ArticleModel;
